/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package morenoapp.components.thinkgraph.node;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.nodes.PPath;
import java.awt.BasicStroke;
import java.awt.Paint;
import java.util.Random;

/**
 *
 * @author root
 */
public class VectorNode extends PNode {

    static final Random random = new Random();
    //coordinates
    float startX;
    float startY;
    float endX;
    float endY;
    //visual
    private PPath lineBody;
    private PPath lineLeftLeaf;
    private PPath lineRightLeaf;
//    private float centerOffsetX1;
//    private float centerOffsetY1;
//    private float centerOffsetX2;
//    private float centerOffsetY2;
    private PPath pointOne;
    private PPath pointTwo;

    public VectorNode(double _startX, double _startY, double _endX, double _endY, float _startOffset, float _endOffset, VectorNode _previous) {
        super();
        final float leafLength = 9.0f;
        startX = (float)_startX;
        startY = (float)_startY;
        endX = (float)_endX;
        endY = (float)_endY;
        double lengthX = _endX - _startX;
        double lengthY = _endY - _startY;
        float length = (float) Math.sqrt(lengthX * lengthX + lengthY * lengthY);
        if (length == 0) {
            return;
        }
        double angle = Math.atan2(lengthY, lengthX);

        float realStartOffset = _startOffset;
        if ((_startOffset + _endOffset) * 1.5 < length) {
            length -= _endOffset;
        } else {
            realStartOffset = 0.0f;
        }

        System.out.println("angle = " + angle); //NOI18N
        float realStartX = (float) (_startX + realStartOffset * Math.cos(angle));
        float realStartY = (float) (_startY + realStartOffset * Math.sin(angle));
        float realEndX = (float) (_startX + length * Math.cos(angle));
        float realEndY = (float) (_startY + length * Math.sin(angle));

//        if (_previous != null && _previous.pointOne != null) {
//            this.pointOne = createPoint(
//                    startX - (float) _previous.pointOne.getX() + _previous.startX,
//                    startY - (float) _previous.pointOne.getY() + _previous.startY);
//        } else {
//            this.pointOne = createPoint(realStartX + (realEndX - realStartX) * 0.33f, realStartY + (realEndY - realStartY) * 0.33f);
//        }
//        if (_previous != null && _previous.pointTwo != null) {
//            this.pointTwo = createPoint(
//                    (float)_endX+(float)_previous.pointTwo.getX()-(float)_previous.getX(),
//                    (float)_endY+(float)_previous.pointTwo.getY()-(float)_previous.getY());
//        } else {
//            this.pointTwo = createPoint(realStartX + (realEndX - realStartX) * 0.67f,realStartY + (realEndY - realStartY) * 0.67f);
//        }

//            this.centerOffsetX1 = _previous.centerOffsetX1;
//            this.centerOffsetY1 = _previous.centerOffsetY1;
//            this.centerOffsetX2 = _previous.centerOffsetX2;
//            this.centerOffsetY2 = _previous.centerOffsetY2;

//            final float radius = 25.0f;
//            centerOffsetX1=(random.nextFloat()-0.5f)*radius;
//            centerOffsetY1=(random.nextFloat()-0.5f)*radius;
//            centerOffsetX2=(random.nextFloat()-0.5f)*radius;
//            centerOffsetY2=(random.nextFloat()-0.5f)*radius;

//        float realCenter1X = (realStartX+realEndX)/2+pointOne.getX();
//        float realCenter1Y = (realStartY+realEndY)/2+centerOffsetY1;
//        float realCenter2X = (realStartX+realEndX)/2+centerOffsetX2;
//        float realCenter2Y = (realStartY+realEndY)/2+centerOffsetY2;


//        lineBody = PPath.createLine(realStartX, realStartY, realEndX, realEndY);
//          lineBody = PPath.createPolyline(
//                  new float[]{realStartX, realCenterX,realEndX},
//                  new float[]{realStartY, realCenterY, realEndY});
        lineBody = new PPath();
        lineBody.moveTo(realStartX, realStartY);
//        lineBody.curveTo(realCenter1X,realCenter1Y,
//                realCenter2X, realCenter2Y,
//                realEndX,realEndY);
        lineBody.lineTo(realEndX, realEndY);
        //lineBody.setStroke(new BasicStroke(3));

//        lineBody.curveTo(
//                (float) pointOne.getX(), (float) pointOne.getY(),
//                (float) pointTwo.getX(), (float) pointTwo.getY(),
//                realEndX, realEndY);


        lineLeftLeaf = PPath.createLine(realEndX, realEndY,
                (float) (realEndX + leafLength * (float) Math.cos(angle + Math.PI + 0.15)),
                (float) (realEndY + leafLength * (float) Math.sin(angle + Math.PI + 0.15)));
        lineRightLeaf = PPath.createLine(realEndX, realEndY,
                (float) (realEndX + leafLength * (float) Math.cos(angle + Math.PI - 0.15)),
                (float) (realEndY + leafLength * (float) Math.sin(angle + Math.PI - 0.15)));


        addChild(lineBody);
        addChild(lineLeftLeaf);
        addChild(lineRightLeaf);
        //addChild(pointOne);
        //addChild(pointTwo);

        setPickable(false);
        lineBody.setPickable(false);
        lineLeftLeaf.setPickable(false);
        lineRightLeaf.setPickable(false);
    }

    @Override
    public void setPaint(Paint newPaint) {
        super.setPaint(newPaint);
        if (lineBody != null) {
            System.out.println("color " + newPaint); //NOI18N
            lineBody.setStrokePaint(newPaint);
            lineLeftLeaf.setStrokePaint(newPaint);
            lineRightLeaf.setStrokePaint(newPaint);
        }
    }

    private static PPath createPoint(float _x, float _y) {
        final float radii = 3.0f;
        return PPath.createEllipse(_x - radii, _y - radii, radii * 2, radii * 2);
    }
}
